package de.pernpas.calendar.service;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailService {
	private final Logger log = Logger.getLogger(MailService.class.getName());

	public MailService(){}
	
	public void sendMail(String msgBody, String recipient, String subject){
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("stinky.nerdy@googlemail.com", ""));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(recipient, ""));
            msg.setSubject(subject, "UTF-8");
            msg.setText(msgBody, "UTF-8");
            msg.setHeader("Content-Type", "text/plain; charset=UTF-8");
            Transport.send(msg);
    
        } catch (AddressException e) {
            log.warning("E-Mail an " + recipient + " konnte nicht versendet werden.");
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} 
	}
	
}
